//
// Created by 17775 on 2024/10/17.
//

#include <thread>
#include <QtGui>
#include <vector>
#include "Single_PowerProfile.h"

//std::shared_ptr<PowerProfileData<std::vector<float>>> Single_PowerProfile::getPowerProfileData() {
//    bool powerProfileIsAvailable = false, powerProfileIsValid = false;
//    std::shared_ptr<PowerProfileData<std::vector<float>>> powerData;
////    RayCi_Single_PowerProfile_isAvailable(_iSingleDocId, &powerProfileIsAvailable);
//    RayCi_Single_PowerProfile_isValid(_iSingleDocId, &powerProfileIsValid);
//    if(powerProfileIsValid) {
//        int powerProfileDataLength = 0;
//        RayCi_Single_PowerProfile_getDataLength(_iSingleDocId, &powerProfileDataLength);
//        if (powerProfileDataLength != 0) {
//            std::vector<float> vecPower;
//            std::vector<float> vecDiameter;
//            vecPower.resize(powerProfileDataLength);
//            vecDiameter.resize(powerProfileDataLength);
//            RayCi_Single_PowerProfile_getDataAsBinary(_iSingleDocId, &vecDiameter[0], &powerProfileDataLength, &vecPower[0], &powerProfileDataLength);
//            powerData = std::make_shared<PowerProfileData<std::vector<float>>>(vecDiameter, vecPower);
//        }
//    }
//    return powerData;
//}

std::shared_ptr<PowerProfileData<std::vector<float>>> Single_PowerProfile::getPowerProfileData() {
    bool powerProfileIsAvailable = false, powerProfileIsValid = false;
    std::shared_ptr<PowerProfileData<std::vector<float>>> powerData;
//    RayCi_Single_PowerProfile_isAvailable(_iSingleDocId, &powerProfileIsAvailable);
    RayCi_Single_PowerProfile_isValid(_iSingleDocId, &powerProfileIsValid);
    if(powerProfileIsValid) {
        int powerProfileDataLength = 0;
        RayCi_Single_PowerProfile_getDataLength(_iSingleDocId, &powerProfileDataLength);
        if (powerProfileDataLength != 0) {
            std::vector<float> vecDiameter(powerProfileDataLength);
            std::vector<float> vecPower(powerProfileDataLength);
            RayCi_Single_PowerProfile_getDataAsBinary(
                    _iSingleDocId,
                    vecDiameter.data(),
                    &powerProfileDataLength,
                    vecPower.data(),
                    &powerProfileDataLength
            );
            bool bValid = false;
            RayCi_Single_Analysis_Statistic_Power_isValid(
                    _iSingleDocId, false, &bValid
            );
            if (bValid) {
                double dPower = 0;
                RayCi_Single_Analysis_Statistic_Power_getPower(
                        _iSingleDocId, false, &dPower
                );
                if (dPower != 0 && dPower >= 1) {
                    std::transform(
                            vecPower.begin(),
                            vecPower.end(),
                            vecPower.begin(),
                            [dPower](float val) { return val / dPower * 100; }
                    );
                }
            }
            powerData = std::make_shared<PowerProfileData<std::vector<float>>>(vecDiameter, vecPower);
        }
    }
    return powerData;
}

double Single_PowerProfile::getCursorPosition() const {
    double dPosition = 0;
    RayCi_Single_PowerProfile_Cursor_getPosition(_iSingleDocId, &dPosition);
    return dPosition;
}

double Single_PowerProfile::getSinglePowerFraction() {
    double dPower = 0;
    double dCurPos = 0;
    RayCi_Single_Analysis_Statistic_Power_getPower(_iSingleDocId, false, &dPower); // 实际功率
    RayCi_Single_PowerProfile_Cursor_getPowerContent(_iSingleDocId, &dCurPos);
    dCurPos = dCurPos / (dPower - 41500) * 100; // 存在硬编码问题
    dCurPos = std::floor(dCurPos * 100.0) / 100.0;
    return dCurPos;
}

void Single_PowerProfile::setCursorPosition(double pos) const {
    RayCi_Single_PowerProfile_Cursor_setPosition(_iSingleDocId, pos);
}

void Single_PowerProfile::setCursorPowerFraction(double dFraction) const {
    RayCi_Single_PowerProfile_Cursor_setPowerFraction(_iSingleDocId, dFraction);
}

QString Single_PowerProfile::getUnitSpatial() const {
    char szUnitSpatial_buffer[10];
    int nSize = sizeof(szUnitSpatial_buffer);
    RayCi_Single_PowerProfile_Cursor_getUnitSpatialA(_iSingleDocId, szUnitSpatial_buffer, &nSize);
    return QString::fromUtf8(szUnitSpatial_buffer);
}

void Single_PowerProfile::setPosition_px(const QString& unit, double sizeX, double sizeY, double dPosX, double dPosY) {
    auto xFactor = unitConversion(unit, sizeX);
    auto yFactor = unitConversion(unit, sizeY);
    x_pos = dPosX * xFactor;
    y_pos = dPosY * yFactor;
    RayCi_Single_PowerProfile_Settings_setPosition_px(_iSingleDocId, x_pos, y_pos);
    emit refreshCenter(x_pos, y_pos);
}

double Single_PowerProfile::unitConversion(const QString& unit, double size) {
    const std::unordered_map<QString, double> conversionStep {
            {"px", 1.0},
            {"um", 1 / size},
            {"mm", 1000.0 / size},
            {"cm", 10000.0 / size},
            {"in", 10000.0 / 0.3937 / size}
    };
    double factor = 1.0;
    if (conversionStep.find(unit) != conversionStep.end()) {
        factor = conversionStep.at(unit);
    } else {
        factor = 1000000.0 / size;
    }
    return factor;
}

double Single_PowerProfile::getViewCenterXPosition_px() {
    RayCi_Single_PowerProfile_Settings_getPositionX_px(_iSingleDocId, &_positionX_px);
    return _positionX_px;
}

double Single_PowerProfile::getViewCenterYPosition_px() {
    RayCi_Single_PowerProfile_Settings_getPositionY_px(_iSingleDocId, &_positionY_px);
    return _positionY_px;
}

//void Single_PowerProfile::getCursorPowerFraction() {
//    double dPower = 0;
//    bool bValid = false;
//    RayCi_LiveMode_Analysis_Statistic_Power_getPower(_iSingleDocId, 0, false, &dPower); // 实际功率
//    RayCi_LiveMode_Analysis_CombinedStatistic_Power_isValid(_iSingleDocId, 0, false, &bValid);
//    if (bValid) {
//        RayCi_LiveMode_Analysis_CombinedStatistic_Power_getPower(_iSingleDocId, 0, false, &dPower);
//    }
//    double dCurPos = 0;
//    if (_bIsGettingPowerPosition) {
//        RayCi_LiveMode_PowerProfile_Cursor_getPosition(_iSingleDocId, &dCurPos);
//    } else {
//        RayCi_LiveMode_PowerProfile_Cursor_getPowerContent(_iSingleDocId, &dCurPos);
//        dCurPos = dCurPos / (dPower - 41500) * 100;
//        dCurPos = std::floor(dCurPos * 100.0) / 100.0;
//    }
//    emit setFractionValue(bPosition, dCurPos);
//    std::this_thread::sleep_for(std::chrono::milliseconds(20));
//}